package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class RandomPickIndex {

	private HashMap<Integer, List<Integer>> map = new HashMap<>();
	private Random random;

	public static void main(String[] args) {
		int[] index = {1};
		RandomPickIndex object = new RandomPickIndex(index);
		object.pick(1);
	}
	
    public RandomPickIndex(int[] nums) {
    	random = new Random();
        for(int i = 0; i < nums.length; i++){
        	if(map.containsKey(nums[i])){
                List<Integer> list = map.get(nums[i]);
        	    list.add(i);
            }else{
                List<Integer> list =  new ArrayList<Integer>();
                list.add(i);
                map.put(nums[i], list);
            }
        }
    }
    
    public int pick(int target) {
    	List<Integer> list = map.get(target);
    	System.out.println(list.size());
    	return list.get(random.nextInt(list.size()));
    }
}
